﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VGS {
    public partial class TeamSettingsForm : Form {
        public TeamSettingsForm() {
            InitializeComponent();
        }

        private void TeamSettings_Load(object sender, EventArgs e) {
            VolleyMatch vm = VolleyMatch.Instance;

            textBoxTeam1.Text = vm.HomeTeam.Name;
            textBoxTeam2.Text = vm.AwayTeam.Name;

            foreach (Player p in vm.HomeTeam.Players) {
                dataGridViewTeam1.Rows.Add(p.Shirt, p.Name);
            }

            foreach (Player p in vm.AwayTeam.Players) {
                dataGridViewTeam2.Rows.Add(p.Shirt, p.Name);
            }

            for (int i = 0; i < dataGridViewTeam1.Rows.Count; i++)
            {
                if (!dataGridViewTeam1.Rows[i].IsNewRow)
                {
                    dataGridViewTeam1.Rows[i].Cells[0].ReadOnly = true;
                    dataGridViewTeam1.Rows[i].Cells[0].Style.BackColor = Color.LightGray;
                }
            }

            for (int i = 0; i < dataGridViewTeam2.Rows.Count; i++)
            {
                if (!dataGridViewTeam2.Rows[i].IsNewRow)
                {
                    dataGridViewTeam2.Rows[i].Cells[0].ReadOnly = true;
                    dataGridViewTeam2.Rows[i].Cells[0].Style.BackColor = Color.LightGray;
                }
            }
        }


        /// <summary>
        /// When the form is closing, I store all data that has been entered
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TeamSettings_FormClosing(object sender, FormClosingEventArgs e)
        {
            VolleyMatch vm = VolleyMatch.Instance;
            
            vm.HomeTeam.Name = textBoxTeam1.Text;
            vm.AwayTeam.Name = textBoxTeam2.Text;
        }

        private void dataGridViewTeam1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1 && !dataGridViewTeam1.Rows[e.RowIndex].IsNewRow)
            {
                VolleyMatch vm = VolleyMatch.Instance;

                // A player's shirt was edited
                if (e.ColumnIndex == 0)
                {
                    string shirt = Convert.ToString(dataGridViewTeam1.Rows[e.RowIndex].Cells[0].Value);
                    string name = Convert.ToString(dataGridViewTeam1.Rows[e.RowIndex].Cells[1].Value);

                    // Make sure some shirt is specified
                    if (!string.IsNullOrEmpty(shirt))
                    {
                        if (!vm.HomeTeam.Players.Exists(pl => pl.Shirt == shirt))
                        {
                            // Add a player with the specified shirt number
                            vm.HomeTeam.Players.Add(new Player(shirt, name));

                            // Make the cell readonly, since it is the shirt that represents
                            // the player
                            dataGridViewTeam1.Rows[e.RowIndex].Cells[0].ReadOnly = true;
                            dataGridViewTeam1.Rows[e.RowIndex].Cells[0].Style.BackColor = Color.LightGray;
                        }
                    }
                }
                // A player's name was edited
                else if (e.ColumnIndex == 1)
                {
                    string shirt = Convert.ToString(dataGridViewTeam1.Rows[e.RowIndex].Cells[0].Value);
                    string name = Convert.ToString(dataGridViewTeam1.Rows[e.RowIndex].Cells[1].Value);

                    if (!string.IsNullOrEmpty(shirt))
                    {
                        if (vm.HomeTeam.Players.Exists(pl => pl.Shirt == shirt))
                        {
                            vm.HomeTeam.Players[shirt].Name = name;
                        }
                    }
                }                    
            }
        }

        private void dataGridViewTeam2_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1 && !dataGridViewTeam2.Rows[e.RowIndex].IsNewRow)
            {
                VolleyMatch vm = VolleyMatch.Instance;

                // A player's shirt was edited
                if (e.ColumnIndex == 0)
                {
                    string shirt = Convert.ToString(dataGridViewTeam2.Rows[e.RowIndex].Cells[0].Value);
                    string name = Convert.ToString(dataGridViewTeam2.Rows[e.RowIndex].Cells[1].Value);

                    // Make sure some shirt is specified
                    if (!string.IsNullOrEmpty(shirt))
                    {
                        if (!vm.AwayTeam.Players.Exists(pl => pl.Shirt == shirt))
                        {
                            // Add a player with the specified shirt number
                            vm.AwayTeam.Players.Add(new Player(shirt, name));

                            // Make the cell readonly, since it is the shirt that represents
                            // the player
                            dataGridViewTeam2.Rows[e.RowIndex].Cells[0].ReadOnly = true;
                            dataGridViewTeam2.Rows[e.RowIndex].Cells[0].Style.BackColor = Color.LightGray;
                        }
                    }
                }
                // A player's name was edited
                else if (e.ColumnIndex == 1)
                {
                    string shirt = Convert.ToString(dataGridViewTeam2.Rows[e.RowIndex].Cells[0].Value);
                    string name = Convert.ToString(dataGridViewTeam2.Rows[e.RowIndex].Cells[1].Value);

                    if (!string.IsNullOrEmpty(shirt))
                    {
                        if (vm.AwayTeam.Players.Exists(pl => pl.Shirt == shirt))
                        {
                            vm.AwayTeam.Players[shirt].Name = name;
                        }
                    }
                }
            }
        }

        private void dataGridViewTeam1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            VolleyMatch vm = VolleyMatch.Instance;
            string shirt = Convert.ToString(e.Row.Cells[0].Value);
            string name = Convert.ToString(e.Row.Cells[1].Value);

            if (!string.IsNullOrEmpty(shirt))
            {
                Player p = vm.HomeTeam.Players[shirt];
                bool inLineup = false;
                bool hasStats = false;

                // Check if the player is in some lineup
                foreach (Set set in vm.Sets)
                {
                    if (set.Lineups.HomeTeam.Exists(p))
                    {
                        inLineup = true;
                        break;
                    }
                }

                // Check if the player has associated stats, i.e. has been playing and scoring
                if (p.Stats.Count > 0)
                    hasStats = true;

                if (hasStats)
                {
                    e.Cancel = true;
                    MessageBox.Show("The player has statistics associated to it",
                        "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (inLineup)
                {
                    e.Cancel = true;
                    MessageBox.Show("The player is added to some lineup and can therefore not be removed",
                        "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                vm.HomeTeam.Players.RemoveAll(pl => pl.Shirt == shirt);
            } 
        }

        private void dataGridViewTeam2_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            VolleyMatch vm = VolleyMatch.Instance;
            string shirt = Convert.ToString(e.Row.Cells[0].Value);
            string name = Convert.ToString(e.Row.Cells[1].Value);

            if (!string.IsNullOrEmpty(shirt))
            {
                Player p = vm.AwayTeam.Players[shirt];
                bool inLineup = false;
                bool hasStats = false;
                
                // Check if the player is in some lineup
                foreach (Set set in vm.Sets)
                {
                    if (set.Lineups.AwayTeam.Exists(p))
                    {
                        inLineup = true;
                        break;
                    }
                }

                // Check if the player has associated stats, i.e. has been playing and scoring
                if (p.Stats.Count > 0)
                    hasStats = true;

                if (hasStats)
                {
                    e.Cancel = true;
                    MessageBox.Show("The player has statistics associated to it",
                        "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (inLineup)
                {
                    e.Cancel = true;
                    MessageBox.Show("The player is added to some lineup and can therefore not be removed",
                        "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                vm.AwayTeam.Players.RemoveAll(pl => pl.Shirt == shirt);
            }
        }
    }
}
